package com.lanmaoly.test.thread;

import com.lanmaoly.test.NamedThreadFactory;

import java.util.concurrent.*;

public class ThreadTest2 {

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,
                10,
                0,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000),
                new NamedThreadFactory("test-am"),
                new ThreadPoolExecutor.CallerRunsPolicy());

        // 这个方法会初始化 核心线程
        int i = threadPoolExecutor.prestartAllCoreThreads();
        System.out.println(i);
//        boolean b = threadPoolExecutor.prestartCoreThread();
//        System.out.println(b);
        for (int j = 0; j < 6; j++) {
            Future<String> future = threadPoolExecutor.submit(() -> {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "sge-" + j);
//            try {
//                String o = future.get();
//                System.out.println(o);
//            } catch (InterruptedException |ExecutionException e) {
//                e.printStackTrace();
//            }
        }
        System.out.println("------------------------------------");
        int activeCount = threadPoolExecutor.getActiveCount();
        long taskCount = threadPoolExecutor.getTaskCount();
        int poolSize = threadPoolExecutor.getPoolSize();
        int largestPoolSize = threadPoolExecutor.getLargestPoolSize();
        long completedTaskCount = threadPoolExecutor.getCompletedTaskCount();
        System.out.println("需要执行的任务数量：" + taskCount);
        System.out.println("已完成的任务数量：" + completedTaskCount);
        System.out.println("线程创建最大历史值：" + largestPoolSize);
        System.out.println("线程池数量：" + poolSize);
        System.out.println("活动线程数量：" + activeCount);
        threadPoolExecutor.shutdown();
        System.out.println("------------------------------------");
//        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(2);
//        for (int j = 0; j < 3; j++) {
//            boolean offer = queue.offer(j);
//            System.out.println(offer);
//        }
        int i1 = Runtime.getRuntime().availableProcessors();
        System.out.println(i1);
    }
}
